﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002B3 RID: 691
	internal class FromEventObservable_<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000F0C RID: 3852 RVA: 0x00046254 File Offset: 0x00044654
		public FromEventObservable_(Action<Action<T>> addHandler, Action<Action<T>> removeHandler) : base(false)
		{
			this.addHandler = addHandler;
			this.removeHandler = removeHandler;
		}

		// Token: 0x06000F0D RID: 3853 RVA: 0x0004626C File Offset: 0x0004466C
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			FromEventObservable_<T>.FromEvent fromEvent = new FromEventObservable_<T>.FromEvent(this, observer);
			return (!fromEvent.Register()) ? Disposable.Empty : fromEvent;
		}

		// Token: 0x040008BC RID: 2236
		private readonly Action<Action<T>> addHandler;

		// Token: 0x040008BD RID: 2237
		private readonly Action<Action<T>> removeHandler;

		// Token: 0x020002B4 RID: 692
		private class FromEvent : IDisposable
		{
			// Token: 0x06000F0E RID: 3854 RVA: 0x00046297 File Offset: 0x00044697
			public FromEvent(FromEventObservable_<T> parent, IObserver<T> observer)
			{
				this.parent = parent;
				this.observer = observer;
				this.handler = new Action<T>(this.OnNext);
			}

			// Token: 0x06000F0F RID: 3855 RVA: 0x000462C0 File Offset: 0x000446C0
			public bool Register()
			{
				try
				{
					this.parent.addHandler(this.handler);
				}
				catch (Exception error)
				{
					this.observer.OnError(error);
					return false;
				}
				return true;
			}

			// Token: 0x06000F10 RID: 3856 RVA: 0x00046310 File Offset: 0x00044710
			private void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000F11 RID: 3857 RVA: 0x0004631E File Offset: 0x0004471E
			public void Dispose()
			{
				if (this.handler != null)
				{
					this.parent.removeHandler(this.handler);
					this.handler = null;
				}
			}

			// Token: 0x040008BE RID: 2238
			private readonly FromEventObservable_<T> parent;

			// Token: 0x040008BF RID: 2239
			private readonly IObserver<T> observer;

			// Token: 0x040008C0 RID: 2240
			private Action<T> handler;
		}
	}
}
